<!---
Name:        addeditcateg.cfm
Author:      Dmitry Svito (prostotelo@gmail.com)
Description: add or edit category
Created:     25.03.2011
--->

<cfparam name="Categ" type="integer" default="0">
<cfparam name="CatN" type="string" default=" ">
<cfparam name="AddFlag" type="integer" default="0">
<cfparam name="Action" type="string" default="addeditcateg.cfm">
<cfparam name="Form.Operation" type="string" default="0">
<cfparam name="Form.CatNam" type="string" default=" ">

<cfif NOT IsDefined("URL.cat") AND NOT IsDefined("URL.add")>
	<cflocation url="categories.cfm">
<cfelseif IsDefined("URL.cat")>
	<cfset Categ = "#URL.cat#">
<cfelse>
	<cfset AddFlag = "#URL.add#">
</cfif>

<!---Categories--->
<cfset CatObjDAO = createObject("component", "Catalog.DataAccessLayer.CategoriesDAO").init() />
<cfset CategoryRow = CatObjDAO.read(Categ)>

<cfset CatN = CategoryRow.getCategoryName()>

<cfif AddFlag IS 1>
	<cfset Action = "addeditcateg.cfm?add=#AddFlag#">
<cfelse>
	<cfset Action = "addeditcateg.cfm?cat=#Categ#">
</cfif>

<html>
<head>
	<title>
		Joe's Catalog
    	<cfif AddFlag IS 1>
			Add category
		<cfelse>
			Edit category
    	</cfif>
	</title>
</head>
<body>
<cfform name="addeditcatform" action="#Action#" method="post" accept-charset="utf-8">
<table width="15%" border="0" cellspacing="2" cellpadding="2" summary="table">
    <tr>
        <td colspan="2" align="center">
        	<cfif AddFlag IS 1>
				Add category
			<cfelse>
				Edit category
        	</cfif>
        </td>
    </tr>
    <tr>
        <td colspan="2" align="center">
			<cfinput type="text" name="CatNam" value="#CatN#"
				maxlength="24"
	            required="yes"
	            message="Category name is required!"
	            validateAt="onSubmit">						
        </td>
    </tr>
    <tr>
        <td align="right">
       		<cfinput type="submit" name="Operation" value="Save">
        </td>
        <td>
        	<cfinput type="button" name="Operation" value="Cancel" onclick="goto()">				
        </td>
    </tr>
</table>
</cfform>

<cfif Form.Operation IS "Save">
	<cfif AddFlag IS 1>
		<cfset CatObjDAO.create(Form.CatNam)>
	<cfelse>
		<cfset CatObjDAO.update(Categ, Form.CatNam)>
	</cfif>
	<cflocation url="categories.cfm">
</cfif>
</body>
</html>
<script>
	function goto()
	{		
		location.replace("categories.cfm");
	}
</script>